<?php defined('BASEPATH') OR exit('No direct script access allowed');

	class system_setting_class extends CI_Model {
	
		public function __construct(){
			$settings = $this->db->get('jtn_system_settings')->result_array();
			foreach($settings as $key=>$value) {
				$this->session->set_userdata($value['strVariable'],$value['strValue']);
			}
		
			//print_r($this->session->all_userdata());
		}
		public function list_system_settings(){
			if(isset($post['intID'])){
				$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
				$this->db->order_by("intID", "asc");
				$query = $this->db->get_where('jtn_system_settings',array('intDisabled'=>0,'intID'=>$intID));
			} else {
				$this->db->order_by("intID", "asc");
				$query = $this->db->get_where('jtn_system_settings',array('intDisabled'=>0));
			}
			if ($query->num_rows() > 0){
				$result = $query->result_array();
				return $result;
			}
			else{
				return 0;
			}
		}	
		public function get_system_settings($post){
			$intID=0;
			if(isset($post['intID'])){
				$intID = $post['intID'];
			}
			$intID = stripslashes(filter_var($intID, FILTER_SANITIZE_STRING));
			$query = $this->db->get_where('jtn_system_settings',array('intID'=>$intID))->result_array();
			$query = current($query);
				
			if(trim($query['intID'])=="") {
				return 0;
			} else {
				return $query;
			}
		}
		public function update($post) {
			$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
			$strValue = stripslashes(filter_var($post['strValue'], FILTER_SANITIZE_STRING));
		
			$data = array(
					'strValue' => $strValue,
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
			);
		
			$this->db->where(array('intID'=>$intID));
			$this->db->update('jtn_system_settings', $data);
		
			if($this->db->_error_number()==0) {
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully saved.";
				$message['intID'] = $this->db->insert_id();
			} else {
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}		
		public function delete($post) {
			$intID = stripslashes(filter_var($post['intID'], FILTER_SANITIZE_STRING));
		
			$data = array(
					'intDisabled' => 1,
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
			);
		
			$this->db->where(array('intID'=>$intID));
			$this->db->update('jtn_system_settings', $data);
		
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully deleted.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}		
	} // end class
?>